#include "bullet.h"
#include "enemy.h"
#include "turret.h"
#include <QPainter>
#include <QDebug>
#include <QPropertyAnimation>
#include <QVector2D>
#include <QtMath>
Bullet::Bullet(int power,int type, int targetType, Game *game,QPoint pos, Enemy *enemy, Turret *turret)
{
    this->game=game;
    this->type=type;
    this->game=game;
    this->current_enemy=enemy;
    this->current_turret=turret;
    this->targetType=targetType;
    this->pos=pos;
    this->power=power;
    switch(type)
    {
    case 0: {
        gif=new QMovie(":/img/bullet1");
        gif->setSpeed(30);
        gif->start();
        movespeed=30;
        break;
        }
    case 1: {
        gif=new QMovie(":/img/bullet2");
        gif->setSpeed(30);
        gif->start();
        movespeed=20;
        break;
        }
    case 2: {
        gif=new QMovie(":/img/bullet5");
        gif->setSpeed(30);
        gif->start();
        movespeed=10;
        break;
        }
    }

    m_startPos=pos;
    m_targetPos=enemy->getPos();
}

Bullet::~Bullet()
{
    delete gif;
}
void Bullet::hitTarget()
{
    switch(targetType) //0代表目标为敌人 1代表目标为塔
    {
        case 0:
        {
            this->current_enemy->setHp(this->power);
            game->remove_bullet(this);
            break;
        }
        case 1:
        {
            this->current_turret->setHp(this->power);
            game->remove_bullet(this);
            break;
        }
    }
}

void Bullet::move()
{
            QPoint target;
            switch(targetType)
            {
            case 0:
            {
                if(game->check_enemy(current_enemy))
                {
                    target=current_enemy->getPos();break;
                }
                else game->remove_bullet(this);
            }
            case 1:
            {
                if(game->check_turret(current_turret))
                   {
                       target=current_turret->getPos();break;
                   }
                       else game->remove_bullet(this);
            }
            }


            /*double x1=target.x();
            double y1=target.y();
            double x2=pos.x();
            double y2=pos.y();

            double s=sqrt((double)(x1-x2)*(x1-x2)+(double)(y1-y2)*(y1-y2));
            double Cos=(double)(y1-y2)/s;
            double Sin=(double)(x1-x2)/s;
            double next_x=x2+Sin*movespeed;
            double next_y=y2+Cos*movespeed;
            if(next_x>x1&&Cos>0) next_x=x1;
            if(next_x<x1&&Cos<0) next_x=x1;
            if(next_y>y1&&Sin>0) next_y=y1;
            if(next_y<y1&&Sin<0) next_y=y1;

            this->pos=QPoint((int)next_x,(int)next_y);
            qDebug()<<pos;*/

            QVector2D normalized(current_enemy->pos - pos);
            normalized.normalize();
            pos=pos+normalized.toPoint()*movespeed;
            rotationSprite = qRadiansToDegrees(qAtan2(normalized.y(), normalized.x()))+90;
            if((pos-current_enemy->getPos()).manhattanLength()<movespeed)hitTarget();

}

void Bullet::draw(QPainter *painter) const
{
    QPixmap gifc(gif->currentPixmap());
    gifc=gifc.scaled(gifc.width()*2,gifc.height()*2);
    painter->save();
    painter->resetTransform();
    painter->translate(pos);
    QPoint drawPos(-gifc.width()/2,-gifc.height()/2);
    //qDebug()<<"direct="<<direction;
    painter->rotate(rotationSprite);
    painter->drawPixmap(drawPos,gifc);
    painter->resetTransform();
    painter->restore();
}

void Bullet::UpdateMap()
{
    move();
}
